package com.example.queue09.queue;

/**
 * 基于链表实现的队列
 * @auther lijiewei
 * @date 2021/8/23 14:48
 */
public class QueueBasedOnLinkedList {

    //队首
    private Node head = null;
    //队尾
    private Node tail = null;

    /**
     * 入队
     * @param data 数据
     * @return
     * @author lijiewei
     * @date   2021/8/23 14:50
     */
    public void enqueue(String data) {
        Node newNode = new Node(data, null);
        if (null == head) {
            //空队
            head = newNode;
            tail = newNode;
        } else {
            tail.setNext(newNode);
            tail = newNode;
        }
    }

    /**
     * 出队
     * @return
     * @author lijiewei
     * @date   2021/8/23 14:55
     */
    public String dequeue() {
        //空队
        if (head == null) {
            return null;
        }
        String data = head.getData();
        head = head.getNext();
        //当只有一个结点时处理
        if (head == null) {
            tail = null;
        }
        return data;
    }

    /**
     * 打印所有结点
     * @author lijiewei
     * @date   2021/8/23 15:02
     */
    public void printAll() {
        //循环时的指针
        Node loopNode = head;
        while (null != loopNode) {
            System.out.print(loopNode.getData() + " ");
            loopNode = loopNode.getNext();
        }
        System.out.println();
    }
}
